/**
 * @file network.c
 * @brief Implements network releated functions.
 * @author Vamshi Ponnapalli
 */
/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "network.h"

/*******************************************************************************
 * Function implementations
 ******************************************************************************/
uint16 network_readWord(uint8 *pBuffer)
{
  uint16 value;

  value = *pBuffer;
  pBuffer++; 
  value = value << 8;
  value |= *pBuffer;

  return (value);
}
/******************************************************************************/
uint32 network_readDWord(uint8 *pBuffer)
{
  uint32 value;

  value = *pBuffer;
  pBuffer++; 
  value = value << 8;
  
  value |= *pBuffer;
  pBuffer++; 
  value = value << 8;
  
  value |= *pBuffer;
  pBuffer++; 
  value = value << 8;
  value |= *pBuffer;
  
  return value;
}
/******************************************************************************/
void network_writeWord(uint16 word, uint8 *pBuffer)
{
  *pBuffer = word >> 8;
  pBuffer++;
  *pBuffer = word;
}
/******************************************************************************/
void network_writeDWord(uint32 dword, uint8 *pBuffer)
{
  pBuffer += 3;
  
  *pBuffer = dword;
  pBuffer--; 
  dword = dword >> 8;
  
  *pBuffer = dword;
  pBuffer--; 
  dword = dword >> 8;
  
  *pBuffer = dword;
  pBuffer--; 
  dword = dword >> 8;
  
  *pBuffer = dword;
}
/******************************************************************************/
ip_address_t network_intToIPAddress(uint32 address)
{
  ip_address_t ipAddress;
  
  ipAddress.b1 = address;
  ipAddress.b2 = address >> 8;
  ipAddress.b3 = address >> 16;
  ipAddress.b4 = address >> 24;
  
  return(ipAddress);
}
/******************************************************************************/
uint32 network_ipAddressToInt(ip_address_t ipAddress)
{
  uint32 address;
  
  address = ipAddress.b4;
  address = address << 8;
  
  address |= ipAddress.b3;
  address = address << 8;
  
  address |= ipAddress.b2;
  address = address << 8;
  
  address |= ipAddress.b1;
  return(address);
}